package com.servlet;

import com.dao.Employee_infoDao;
import com.dao.impl.Employee_infoDaoImpl;
import com.entry.Employee_info;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.util.PageModel;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet(urlPatterns = "/man.do")
public class Man_update_selectServlet extends HttpServlet {


    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html; charset=utf-8");
        //获取man_uphold页面中的action
        String action = request.getParameter("action");
        Employee_infoDao eImpl = new Employee_infoDaoImpl();
        HttpSession session = request.getSession();
        if ("selectAll".equals(action)) {
            int pageSize = request.getParameter("pageSize") == null ? 5 : Integer.parseInt(request.getParameter("pageSize").toString());
            int pageNo = request.getParameter("pageNo") == null ? 1 : Integer.parseInt(request.getParameter("pageNo").toString());
            PageModel<Employee_info> pm = eImpl.findAll(pageSize, pageNo);
            pm.setPageNav(request.getRequestURI());
            session.setAttribute("pm", pm);
            response.sendRedirect("./man_manage/man_uphold.jsp");
        } else if ("update".equals(action)) {
            JsonParser jParser = new JsonParser();
            JsonObject jt = (JsonObject) jParser.parse(request.getParameter("up_info"));//构造JsonObject对象
            Employee_info e = new Employee_info();
            String username = jt.get("username").getAsString();
            String idcode = jt.get("idcode").getAsString();
            String nation = jt.get("nation").getAsString();
            String political = jt.get("political").getAsString();
            String homeaddr = jt.get("homeaddr").getAsString();
            e.setUsername(username);
            e.setIdcode(idcode);
            e.setNation(nation);
            e.setPolitical(political);
            e.setHomeaddr(homeaddr);
            int i = eImpl.update(e);
            if (i > 0) {
                session.setAttribute("Mess", "更新成功！");
                response.sendRedirect("./man_manage/man_uphold.jsp");
            } else {
                request.setAttribute("Mess", "更新失败！");
                response.sendRedirect("./man_manage/man_update.jsp");
            }
        } else if ("delete".equals(action)) {
            String username = request.getParameter("username");
            int i = eImpl.delete(username);
            if (i > 0) {
                session.setAttribute("Mess", "删除成功！");
            } else {
                session.setAttribute("Mess", "删除失败！");
            }
            response.sendRedirect("./man_manage/man_uphold.jsp");
        } else if ("selectONE".equals(action)) {
            int pageSize = request.getParameter("pageSize") == null ? 5 : Integer.parseInt(request.getParameter("pageSize").toString());
            int pageNo = request.getParameter("pageNo") == null ? 1 : Integer.parseInt(request.getParameter("pageNo").toString());
            if (request.getParameter("pageNo") == null || request.getParameter("pageSize") == null){
                session.removeAttribute("key");
            }
            JsonObject jt = null;
            JsonParser jParser = new JsonParser();
            if(session.getAttribute("key") == null){
                jt = (JsonObject) jParser.parse(request.getParameter("key"));
            }else{
                jt = (JsonObject) jParser.parse(String.valueOf(session.getAttribute("key")));
            }
            String key = jt.get("key").getAsString();
            String s = request.getParameter("key");
            System.out.println(key);
            JsonObject language1=new JsonObject();
            language1.addProperty("key", key);
            PageModel<Employee_info> pm = null;
            pm = eImpl.findByOne(pageSize, pageNo, key);
            pm.setPageNav(request.getRequestURI()+"?action=selectONE&key=" + language1);
            session.setAttribute("key",language1);
            session.setAttribute("pm", pm);
            response.sendRedirect("./man_manage/man_uphold.jsp");
//            PageModel<Employee_info> pm = eImpl.findByOne(pageSize, pageNo, s);
//            pm = eImpl.findByOne(pageSize, pageNo, s);
//            System.out.println(s);
//            pm.setPageNav(request.getRequestURI());
//            session.setAttribute("pm", pm);
//            response.sendRedirect("./man_manage/man_uphold.jsp");
        } else if ("findUser".equals(action)) {
            int pageSize = request.getParameter("pageSize") == null ? 5 : Integer.parseInt(request.getParameter("pageSize").toString());
            int pageNo = request.getParameter("pageNo") == null ? 1 : Integer.parseInt(request.getParameter("pageNo").toString());
            String username = request.getParameter("username");
            PageModel<Employee_info> pm = eImpl.findByOne(pageSize, pageNo, username);
            pm = eImpl.findByOne(pageSize, pageNo, username);
            pm.setPageNav(request.getRequestURI());
            session.setAttribute("pm", pm);
            response.sendRedirect("./man_manage/man_update.jsp");
        }else if ("upgo".equals(action)) {
            String username = request.getParameter("username");
            System.out.println(username);
            Employee_info e = new Employee_info();
            e = eImpl.findUser(username);
            session.setAttribute("info", e);
            response.sendRedirect("./man_manage/man_update.jsp");
        }
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        this.doPost(request, response);
    }
}
